package cn.zhangbo.config;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import cn.zhangbo.cons.LogTemplate;
import cn.zhangbo.dao.StudentMapper;
import cn.zhangbo.dao.po.Student;
import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
//@ConditionalOnBean(StudentMapper.class)
public class AutoGenerateStudentConfiguration {
    StudentMapper studentMapper;
    List<Student> students;

    @Autowired
    AutoGenerateStudentConfiguration(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
        initStudent();
        doSave();
        log.info(String.format(LogTemplate.CONFIG_TEMPLATE, "自动生成100个学生"), AutoGenerateStudentConfiguration.class);
    }

    public void initStudent() {
        students = new ArrayList<Student>();
        for (int i = 1; i < 100; i++) {
            Student temp = new Student();
            temp.setId((long) i);
            temp.setSex(i ^ 1);
            temp.setAge(i);
            temp.setAddress("你知道我住哪吗" + i + (i ^ 2));
            temp.setCreateDatetime(new Date());
            temp.setEmail("swust" + i + "@163.com");
            temp.setName("student+" + i);
            students.add(temp);
        }
    }

    public void doSave() {
        if (studentMapper != null) {
            studentMapper.save(students);
        }
    }
}
